from common_bind_service.common.dao.base_model_dao import BaseModelDao
from common_bind_service.models.code_generation import CodeGenerationRecord, RuleCodeGenerationRecord
from common_bind_service.utils.db import get_code_generation_record, get_rule_code_record


class CodeGenerationDao(BaseModelDao[CodeGenerationRecord]):

    def get_db(self):
        return get_code_generation_record()

    async def get_latest_num_by_query(self, query: dict):
        db = self.get_db()
        return await db.find_one(query, sort=[('latest_num', -1)])


class RuleCodeGenerationDao(BaseModelDao[RuleCodeGenerationRecord]):
    def get_db(self):
        return get_rule_code_record()

    async def get_latest_serial_no_by_query(self, query: dict):
        db = self.get_db()
        return await db.find_one(query, sort=[('serial_no', -1)])

    async def get_latest_data_by_query(self, query: dict):
        db = self.get_db()
        return await db.find_one(query, sort=[('create_time', -1)])
